package com.genokon.service.impl;

import com.genokon.dto.OrderSampleDto;
import com.genokon.dto.SampleDto;
import com.genokon.entry.GeneServiceOrder;
import com.genokon.entry.GeneServicePatientClinical;
import com.genokon.entry.GeneServiceSample;
import com.genokon.mapper.GeneServiceOrderMapper;
import com.genokon.mapper.GeneServicePatientClinicalMapper;
import com.genokon.mapper.GeneServiceSampleMapper;
import com.genokon.service.OrderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @author by baichaoda
 * @date 2021/4/20 11:26
 */
@Service("OrderService")
public class OrderServiceImpl implements OrderService {
    @Resource
    private GeneServiceOrderMapper geneServiceOrderMapper;
    @Resource
    private GeneServiceSampleMapper geneServiceSampleMapper;
    @Resource
    private GeneServicePatientClinicalMapper geneServicePatientClinicalMapper;
    @Override
    public Integer addOrderInfo(OrderSampleDto dto) {
        GeneServiceOrder order = dto.getOrder();
        order.setOrderNo(createOrderNo());
        order.setCreateTime(new Date());
        order.setUpdateTime(new Date());
        order.setFlag(1);
        order.setUpdateUser(order.getCreateUser());
        int count = geneServiceOrderMapper.insert(order);
        String orderId = order.getId();

        dto.getSamples().forEach(item->{
            GeneServiceSample sample = new GeneServiceSample();
            sample.setSampleNo(item.getSampleNo());
            sample.setName(item.getName());
            sample.setSex(item.getSex());
            sample.setBirthday(item.getBirthday());
            sample.setAge(item.getAge());
            sample.setType(item.getType());
            sample.setCollectionTime(item.getCollectTime());
            sample.setFlag(1);
            sample.setCreateUser(dto.getOrder().getCreateUser());
            sample.setUpdateUser(dto.getOrder().getCreateUser());
            sample.setCreateTime(new Date());
            sample.setUpdateTime(new Date());
            geneServiceSampleMapper.insert(sample);

            GeneServicePatientClinical clinical = new GeneServicePatientClinical();
            clinical.setOrderId(orderId);
            clinical.setSampleId(sample.getId());
            clinical.setRelation(item.getRelation());
            clinical.setApplyCause(item.getApplyCause());
            clinical.setFlag(1);
            clinical.setCreateUser(dto.getOrder().getCreateUser());
            clinical.setUpdateUser(dto.getOrder().getCreateUser());
            clinical.setCreateTime(new Date());
            clinical.setUpdateTime(new Date());

            geneServicePatientClinicalMapper.insert(clinical);

        });

        return count;
    }
    private String createOrderNo(){
        Random rand = new Random();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String tmp = simpleDateFormat.format(new Date());
        return tmp + String.format("%06d", (int)(rand.nextDouble()*100000));
    }
}
